<?php
/**
 * 公共feed类
 * 
 * 类名
 * ml_model_dbPublicFeed
 * @zhanglei11
 * 
 */
class ml_model_dbPublicFeed extends Lib_datamodel_db 
{
    
    /**
     * 创建构造函数
     *
     */
    function __construct()
    {
        $db_config = ml_factory::load_standard_conf('db');
        parent::__construct('public_feed' , $db_config['public_feed']);
    }
    
    protected function hook_after_fetch()
    {
        $data = $this->get_data();

        if(isset($data['feed_info']))
        {
            $data['feed_info'] = unserialize($data['feed_info']);
            $this->set_data($data);
        }else
        {
            foreach($data as $key=>$val)
            {
                if(isset($val['feed_info']))
                {
                    $val['feed_info'] =unserialize($val['feed_info']);
                }
                $arr[$key]=$val;
            }
            $this->set_data($arr);
        }

        return true;
    }
    protected function hook_before_write($array)
    {
        if(isset($array['feed_info']))
        {
            $array['feed_info'] = serialize($array['feed_info']);
        }
        return $array;
    }

    /**
     * 增加feed
     * @param array $data
     */
    public function add_feed(  $data )
    {
        
        if(!$this->init_db())
        return false;
        $rs = $this->insert($data);
                
        if(!$rs)
            return false;
        /* 
        else 
        {
            //    ?? 记这个干嘛的
//            $rid = ml_tool_resid::make_resid($uid , ML_RESID_TYPE_ATME , $this->insert_id());
            return false;
        } */
    }
    
    /**
     * 根据uids 获取feed列表
     * @param array $uids    
     * @param int $page
     * @param int $pagesize
     */
    public function getListByUids($uids , $page = 1 , $pagesize = 40)
    {
        if(!$this->init_db('',self::DB_SLAVE))
            return false;
        $uids = implode(',',$uids);
        $start = ($page - 1) * $pagesize;
        $sql = 'select * from `'.$this->table.'` where `uid` in ('.$uids.') order by `ctime` desc limit '.$start.' , '.$pagesize;
        return $this->query($sql,true);
    }
    
    /**
     * 根据uids获取 feed 总数
     * @param array $uids
     * 这玩意儿 是不是得换个方式啊？ 挺费资源的。
     */
    public function getCntByUids($uids )
    {
        if(!$this->init_db('',self::DB_SLAVE))
            return false;
        if(is_array($uids))
            $uids = implode(',',$uids);
        
        $sql = 'select count(*) as num from `'.$this->table.'` where `uid` in ('.$uids.') ';
        return $this->query($sql,true);
    }
    
    /**
     * 根据pubfeed_id删除feed记录
     * @param int $pubfeed_id
     */
    public function delByUserFeedId($userfeed_rid)
    {
        if(!$this->init_db())    //    这个不分表的是不是可以放到 构造函数里？
            return false;
        $sql = 'delete from `'.$this->table.'` where `userfeed_rid` = \''.$userfeed_rid.'\'';
        return $this->query($sql,true);
        
    }
    
    public function delByContent_rid($content_rid){
        if(!$this->init_db())    //    这个不分表的是不是可以放到 构造函数里？
            return false;
        $sql = 'delete from `'.$this->table.'` where `content_rid` = \''.$content_rid.'\'';
        return $this->query($sql,true);
    }
}
?>